var app = getApp()
var WxParse = require('../../wxParse/wxParse.js');

Page({

  data: {
    isShowModal: false,
    chooseGuige: {},
    buyNumber: 1,
    modalOperaType: "buy",

  },
  onLoad: function (e) {
    var that = this;
    if (e && e.scene) {
      var scene = decodeURIComponent(e.scene).split('&');
      console.log(scene);
      var secneObj = {};
      for (var i = 0; i < scene.length; i++) {
        var arr = scene[i].split('=');
        var key = arr[0];
        secneObj[key] = arr[1];
      }
      e = secneObj;
    }
    if (e && e.id) {
      that.setData({
        gid: e.id
      })
    }

    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.loadData();
      that.requestJoinnum();
      that.requestJoinavatar();
      }
    } else {
      that.loadData();
      that.requestJoinnum();
      that.requestJoinavatar();
    }
  },
  onShow:function(){
    var that = this;
    that.setData({
      customerService: app.globalData.customerService ? app.globalData.customerService:'',
      sessionForm: app.globalData.sessionForm ? app.globalData.sessionForm:''
    })
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        gid: that.data.gid,
        map:'applet_appointment_good_detail'
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.ec == 200) {
          var dataInfo = res.data.data;
          dataInfo.limit = parseInt(dataInfo.limit);
          that.setData({
            info: dataInfo,
            goodDetail: res.data.data
          })
          console.log(dataInfo)
          app.setNavtitle(res.data.data.name);
          WxParse.wxParse('article', 'html', res.data.data.detail, that, 5);
        } else {

        }
      },
      fail: function () {
        console.log("fail")
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  // 已参加预约人数数量
  requestJoinnum: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        goods_id: that.data.gid,
        map: 'applet_appointment_join_count'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          that.setData({
            joinCount: res.data.data.count
          })
        } else {

        }
      },
      fail: function () {
        console.log("fail")
      },
      complete: function () {
        
      }
    })
  },
  // 已参加预约头像列表
  requestJoinavatar: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        goods_id: that.data.gid,
        map: 'applet_appointment_join_list',
        page: 0
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          that.setData({
            joinAvatar: res.data.data.list
          })
        } else {

        }
      },
      fail: function () {
        console.log("fail")
      },
      complete: function () {

      }
    })
  },
  //显示购买弹层
  showDetailView: function () {
    var that = this;

    if(!that.data.info){
      return;
    }
    that.setData({
      isShowModal: true
    })
  },
  //隐藏
  hideModal: function () {
    var that = this;
    that.setData({
      isShowModal: false
    })
  },

  reservationDetail: function () {
    var that = this;

    wx.navigateTo({
      url: '/subpages/Generalreservationdetailfillorder/Generalreservationdetailfillorder?id=' + that.data.gid,
    })
  },
  chooseGuige: function (e) {
    var that = this;
    var id = e.target.dataset.id;
    var price = e.target.dataset.price;
    var stock = e.target.dataset.stock;
    var name = e.target.dataset.name;
    that.setData({
      chooseGuige: {
        id: id,
        price: price,
        stock: stock,
        name: name
      }
    })
  },
  //数量
  changeNum: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var buyNum = parseInt(that.data.buyNumber);
    if (type == 'add') {
      buyNum++;
    } else {
      if (buyNum > 1) {
        buyNum--;
      } else {
        buyNum = 1;
      }
    }
    that.setData({
      buyNumber: buyNum
    })
  },
  //下一步
  nextStep: function () {
    var that = this;
    var gfid = that.data.chooseGuige.id;
    if (that.data.goodDetail.format.length != 0 && !that.data.chooseGuige.id) {
      wx.showToast({
        title: '请选择项目',
        image: "/images/hud_info.png",
        duration: 1000,
        mask: true
      })
      return;
    }

    if (that.data.buyNumber == 0) {
      wx.showToast({
        title: '请选择购买数量',
        image: "/images/hud_info.png",
        duration: 1000,
        mask: true
      })
      return;
    }

    wx.navigateTo({
      url: '/subpages/Generalreservationdetailfillorder/Generalreservationdetailfillorder?id=' + that.data.gid + "&num=" + that.data.buyNumber + "&gfid=" + that.data.chooseGuige.id,
    })
    // var isFormat = that.data.goodDetail.format.length > 0;
    // var orderArr = [];
    // var orderObj = {};
    // orderObj.gid = that.data.goodDetail.id;
    // orderObj.gfid = that.data.chooseGuige.id;;
    // orderObj.num = that.data.buyNumber;

    // wx.showLoading({
    //   title: '加载中...',
    // })
    // wx.request({
    //   url: app.getRequestUrl("/applet.php?map=applet_appointment_create_trade"),
    //   data: {
    //     buys: [{ "gid": that.data.gid, "num": that.data.buyNumber, gfid: that.data.chooseGuige.id }],
    //     plum_session_applet: app.globalData.plumSession
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.ec == 200) {
    //       that.setData({
    //         info: res.data.data
    //       })

    //     } else {

    //     }
    //   },
    //   fail: function () {
    //     console.log("fail")
    //   },
    //   complete: function () {
    //     wx.hideLoading()
    //   }
    // })
  },
  toreservationAvatarlist: function () {
    var that = this;
    var gid = that.data.gid;
    wx.navigateTo({
      url: '/subpages0/yuyueAvatarlist/yuyueAvatarlist?gid=' + gid
    })
  },
  showSharemodal(){
    var pageShare = this.selectComponent('.page-share'), 
    posterData={map:'applet_appointment_good_share',id:this.data.gid};
    console.log(posterData)
    this.setData({
      posterData:posterData
    })
    pageShare.showSharemodal()
  },
  hideSharemodal(){
    var pageShare = this.selectComponent('.page-share');
    pageShare.hideSharemodal()
  },
  // 统计转发
  shareTj: function () {
    var that = this;
    var data = {
      map: 'applet_appointment_forward',
      goods_id: that.data.gid
    }
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          
        } else {
          
        }
      },
      complete: function () {
      }
    });
  },
  //转发
  onShareAppMessage: function () {
    var that = this;
    that.hideSharemodal();
    var title = that.data.goodDetail.name;
    var id = that.data.gid;
    that.shareTj();
    app.getPoint(that);
    return {
      title: title,
      path: '/subpages/Generalreservationdetail/Generalreservationdetail?id='+id
    }
  }
})